home *** CD-ROM | disk | FTP | other *** search
- unit GraphsU;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ExtCtrls;
-
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Image1: TImage;
- Button2: TButton;
- Button3: TButton;
- Label1: TLabel;
- Edit1: TEdit;
- Label2: TLabel;
- Button4: TButton;
- procedure Button1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure Button3Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure Button4Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- var
- Root2Pi : double;
- EToMinus1 : double;
- EToMinus2 : double;
-
- function Normal(x : double) : double;
- begin
- Result := exp(-(X*X)/2.0)/Root2Pi;
- end;
-
- function Poisson(x : integer;
- Mean : double) : double;
- var
- MeanPower : double;
- Factorial : double;
- i : integer;
- begin
- if (x = 0) then
- MeanPower := 1.0
- else begin
- MeanPower := Mean;
- if x > 1 then
- for i := 2 to x do
- MeanPower := MeanPower * Mean;
- end;
- if (x <= 1) then
- Result := MeanPower * exp(-Mean)
- else begin
- Factorial := 1.0;
- for i := 2 to x do
- Factorial := Factorial * x;
- Result := MeanPower * exp(-Mean) / Factorial;
- end;
- end;
-
- function XPixel(x : double;
- PixelZero : integer;
- PixelWidth: integer;
- RealWidth : double) : integer;
- begin
- Result := trunc(PixelZero + (x * (PixelWidth / RealWidth)));
- end;
-
- function YPixel(y : double;
- PixelZero : integer;
- PixelHeight: integer;
- RealHeight : double) : integer;
- begin
- Result := trunc(PixelZero - (y * (PixelHeight / RealHeight)));
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- var
- Wd, Ht : integer;
- XAxis, YAxis : integer;
- x, y : double;
- RealWidth : double;
- StopX : double;
- StartTick, TickLen : integer;
- begin
- Wd := Image1.Width;
- Ht := Image1.Height;
- XAxis := Ht - (Ht div 6);
- YAxis := Wd div 2;
- RealWidth := 8.0;
-
- with Image1.Canvas do begin
- {clear it}
- FillRect(Rect(0, 0, Wd, Ht));
- {draw axes}
- Pen.Color := clBlack;
- Pen.Width := 1;
- MoveTo(0, XAxis);
- LineTo(Wd, XAxis);
- MoveTo(YAxis, XAxis);
- LineTo(YAxis, 0);
-
- {draw tick marks on XAxis}
- x := -(RealWidth / 2.0);
- StartTick := XPixel(x, YAxis, Wd, RealWidth);
- if (abs(x - round(x)) < 0.09) then
- TickLen := 5
- else
- TickLen := 3;
- MoveTo(StartTick, XAxis);
- LineTo(StartTick, XAxis - TickLen);
- StopX := -x;
- while x < StopX do begin
- x := x + 0.1;
- StartTick := XPixel(x, YAxis, Wd, RealWidth);
- if (abs(x - round(x)) < 0.09) then
- TickLen := 5
- else
- TickLen := 3;
- MoveTo(StartTick, XAxis);
- LineTo(StartTick, XAxis - TickLen);
- if (TickLen = 5) then
- TextOut(StartTick - 5, XAxis + 5,
- Format('%.1f', [x]));
- end;
-
- {draw tick marks on YAxis}
- y := 0.0;
- StartTick := YPixel(y/10.0, XAxis, XAxis, 0.5);
- if (abs(y - round(y)) < 0.09) then
- TickLen := 5
- else
- TickLen := 3;
- MoveTo(YAxis, StartTick);
- LineTo(YAxis + TickLen, StartTick);
- while y < 5.0 do begin
- y := y + 0.1;
- StartTick := YPixel(y/10.0, XAxis, XAxis, 0.5);
- if (abs(y - round(y)) < 0.09) then
- TickLen := 5
- else
- TickLen := 3;
- MoveTo(YAxis, StartTick);
- LineTo(YAxis + TickLen, StartTick);
- if (TickLen = 5) then
- TextOut(YAxis + TickLen, StartTick,
- Format('%.1f', [y/10.0]));
- end;
-
- Pen.Color := clRed;
- Pen.Width := 2;
- x := -(RealWidth / 2.0);
- y := Normal(x);
- MoveTo(XPixel(x, YAxis, Wd, RealWidth),
- YPixel(y, XAxis, XAxis, 0.5));
- StopX := -x;
- while x < StopX do begin
- x := x + 0.1;
- y := Normal(x);
- LineTo(XPixel(x, YAxis, Wd, RealWidth),
- YPixel(y, XAxis, XAxis, 0.5));
- end;
- TextOut(10, 10, 'Standard Normal Curve');
- end;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Root2Pi := Sqrt(2 * Pi);
- EToMinus1 := exp(-1.0);
- EToMinus2 := exp(-2.0);
- end;
-
- procedure TForm1.Button3Click(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TForm1.Button2Click(Sender: TObject);
- var
- Wd, Ht : integer;
- XAxis, YAxis : integer;
- x, y : double;
- RealWidth : double;
- RealHeight : double;
- StopX : double;
- StartTick, TickLen : integer;
- Mean : double;
- begin
- Mean := StrToFloat(Edit1.Text);
- RealHeight := Poisson(round(Mean/2.0), Mean) * 1.5;
- Wd := Image1.Width;
- Ht := Image1.Height;
- XAxis := Ht - (Ht div 6);
- YAxis := 10;
- RealWidth := 8.0;
-
- with Image1.Canvas do begin
- {clear it}
- FillRect(Rect(0, 0, Wd, Ht));
- {draw axes}
- Pen.Color := clBlack;
- Pen.Width := 1;
- MoveTo(0, XAxis);
- LineTo(Wd, XAxis);
- MoveTo(YAxis, XAxis);
- LineTo(YAxis, 0);
-
- {draw tick marks on XAxis}
- x := 0.0;
- StartTick := XPixel(x, YAxis, Wd, RealWidth);
- if (abs(x - round(x)) < 0.09) then
- TickLen := 5
- else
- TickLen := 3;
- MoveTo(StartTick, XAxis);
- LineTo(StartTick, XAxis - TickLen);
- StopX := RealWidth;
- while x < StopX do begin
- x := x + 0.1;
- StartTick := XPixel(x, YAxis, Wd, RealWidth);
- if (abs(x - round(x)) < 0.09) then
- TickLen := 5
- else
- TickLen := 3;
- MoveTo(StartTick, XAxis);
- LineTo(StartTick, XAxis - TickLen);
- if (TickLen = 5) then
- TextOut(StartTick - 5, XAxis + 5,
- Format('%.1f', [x]));
- end;
-
- {draw tick marks on YAxis}
- y := 0.0;
- StartTick := YPixel(y/10.0, XAxis, XAxis, RealHeight);
- if (abs(y - round(y)) < 0.09) then
- TickLen := 5
- else
- TickLen := 3;
- MoveTo(YAxis, StartTick);
- LineTo(YAxis + TickLen, StartTick);
- while y < (RealHeight*10.0) do begin
- y := y + 0.1;
- StartTick := YPixel(y/10.0, XAxis, XAxis, RealHeight);
- if (abs(y - round(y)) < 0.09) then
- TickLen := 5
- else
- TickLen := 3;
- MoveTo(YAxis, StartTick);
- LineTo(YAxis + TickLen, StartTick);
- if (TickLen = 5) then
- TextOut(YAxis + TickLen, StartTick,
- Format('%.1f', [y/10.0]));
- end;
-
- Pen.Color := clRed;
- Pen.Width := 2;
- x := 0.0;
- y := Poisson(round(x), Mean);
- MoveTo(XPixel(x, YAxis, Wd, RealWidth),
- YPixel(y, XAxis, XAxis, RealHeight));
- StopX := RealWidth;
- while x < StopX do begin
- x := x + 1.0;
- y := Poisson(round(x), Mean);
- LineTo(XPixel(x, YAxis, Wd, RealWidth),
- YPixel(y, XAxis, XAxis, RealHeight));
- end;
- TextOut(100, 10, 'Poisson Curve');
- end;
- end;
-
-
- procedure TForm1.Button4Click(Sender: TObject);
- var
- Mean : double;
- begin
- Mean := StrToFloat(Edit1.Text);
- Mean := Mean + 0.1;
- Edit1.Text := FloatToStr(Mean);
- Button2Click(Sender);
- end;
-
- end.
-